package com.epalmpay.controller.channel.datareport;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.annotation.Resource;

import com.epalmpay.entity.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.epalmpay.commom.BaseController;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.datareport.TransDataReportInDTO;
import com.epalmpay.dto.datareport.TransDataReportListOutDTO;
import com.epalmpay.service.datareport.ITransDataReportService;

/** 
 * 说明：数据报表 :营销数据列表
 * 创建人：gwn
 * 创建时间：2017-06-27
 */

@Controller
@RequestMapping(value="/merchant/transDataReport")
public class TransDataReportController  extends BaseController{
	
	@Resource
	private ITransDataReportService transDataReportService;

	
	@RequestMapping(value="/list")
	@ResponseBody
	public TransDataReportListOutDTO list(TransDataReportInDTO transDataReportInDTO){
		
		TransDataReportListOutDTO transDataReportListOutDTO = new TransDataReportListOutDTO();
		try {
			//查询日的
			if (transDataReportInDTO.getQueryDayOrMonth()==1) {
				if (transDataReportInDTO.getJoinStart()==null) {
					Date date=new Date();//取时间
					Calendar calendar = new GregorianCalendar();
					calendar.setTime(date);
					calendar.add(calendar.DATE,-1);//把日期往前减少一天，若想把日期向后推一天则将负数改为正数
					date=calendar.getTime();
					transDataReportInDTO.setJoinStart(date);
				}
				//刷卡交易金额 扫码付交易金额  台卡收银金额
				StatisShopDay statisShopDay= transDataReportService.queryStatisShopDayData(transDataReportInDTO);
				transDataReportListOutDTO.setStatisShopDay(statisShopDay);
				//线上订单消费
				StatisGroupDay statisGroupDay =transDataReportService.queryStatisGroupDayData(transDataReportInDTO);
				transDataReportListOutDTO.setStatisGroupDay(statisGroupDay);
				//积分 (待...)
				//积分发放点数
				BonusSendLog bonusSendLog = transDataReportService.querySendBonusAmountDay(transDataReportInDTO);
				transDataReportListOutDTO.setBonusSendLog(bonusSendLog);
				//积分核销点数和金额
				TradeOffline tradeOffline = transDataReportService.queryVerifyBonusAmountDay(transDataReportInDTO);
				transDataReportListOutDTO.setTradeOffline(tradeOffline);
				//现金
				TradeOffline tradeOffline2=transDataReportService.queryCash(transDataReportInDTO);
				transDataReportListOutDTO.setTradeOffline2(tradeOffline2);


			}
			
			//查询月的
			if (transDataReportInDTO.getQueryDayOrMonth()!=1) {

				StatisShopMonth statisShopMonth= transDataReportService.queryStatisShopMonthData(transDataReportInDTO);
				transDataReportListOutDTO.setStatisShopMonth(statisShopMonth);
				//线上订单消费
				StatisGroupMonth statisGroupMonth =transDataReportService.queryStatisGroupMonthData(transDataReportInDTO);
				transDataReportListOutDTO.setStatisGroupMonth(statisGroupMonth);

				//上面2个按月查 数据库本身存在字段201705的类型  以下的使用创建时间去匹配月
				transDataReportInDTO.setStringStatisDate(transDataReportInDTO.getStringStatisDate()+"0"+0);
				if(transDataReportInDTO.getStringStatisDateEnd()!=null){

					transDataReportInDTO.setStringStatisDateEnd(transDataReportInDTO.getStringStatisDateEnd()+"0"+'0');
				}
				//积分
				//积分发放点数
				BonusSendLog bonusSendLog = transDataReportService.querySendBonusAmountMonth(transDataReportInDTO);
				transDataReportListOutDTO.setBonusSendLog(bonusSendLog);
				//积分核销点数和金额
				TradeOffline tradeOffline = transDataReportService.queryVerifyBonusAmountMonth(transDataReportInDTO);
				transDataReportListOutDTO.setTradeOffline(tradeOffline);

				//现金
				TradeOffline tradeOffline2=transDataReportService.queryCashMonth(transDataReportInDTO);
				transDataReportListOutDTO.setTradeOffline2(tradeOffline2);


			}
			
			
			transDataReportListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return transDataReportListOutDTO;
		} catch (ApplicationException ae) { 
			transDataReportListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}catch (Exception e) {
			transDataReportListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
				e.printStackTrace();
		}
		return transDataReportListOutDTO;
		
	}
	
	

}
